import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

const state = {
  parentText: "父组件原始文本",
  childText: "子组件原始文本",
  num: 1,
};

const mutations = {
  changeParentText(state, str) {
    state.parentText = str;
  },
  changeChildText(state, str) {
    state.childText = str;
  },
  // 让num累加
  // payload是一个形参，如果组件在commit时，有传这个参数过来，就存在，如果没有传过来，就是undefined
  increase(state, payload) {
    state.num += payload ? payload : 1;
  },
  // 让num累减
  decrease(state) {
    state.num--;
  },
};
const actions = {
  // actions专门用来处理异步，实际修改状态值的，依然是mutations
  // 点击了“减1”按钮后，放慢一秒再执行减法
  decreaseAsync(context) {
    context.commit("decrease");
  },
};
const store = new Vuex.Store({
  state: state,
  mutations: mutations,
  actions: actions,
});

export default store;
